//
//  TestTableViewCell.m
//  hitTestDemo
//
//  Created by 哆啦 on 2019/11/25.
//  Copyright © 2019 哆啦. All rights reserved.
//

#import "TestTableViewCell.h"
#import "TestModel.h"

@interface TestTableViewCell ()

//@property (nonatomic, assign) NSInteger totalCount;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation TestTableViewCell
{
    TestModel *_model;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setUI];
        
    }
    return self;
}

- (void)setUI
{
    [self.contentView addSubview:self.timeLabel];
    //self.totalCount = 1574685505;
    
    self.timeLabel.frame = CGRectMake(50, 10, 300, 30);
    
    //self.timeLabel.text = [self getDateWithTimeInterval:self.totalCount];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)setTestModel:(TestModel *)model
{
    _model = model;
    self.timeLabel.text = [self getDateWithTimeInterval:model.timeInterval];
}


- (NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
            self->_model.timeInterval --;
            self.timeLabel.text = [self getDateWithTimeInterval:self->_model.timeInterval];
            NSLog(@"time:%f",self->_model.timeInterval);
        }];
    }
    return _timer;
}
- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.backgroundColor = [UIColor brownColor];
        _timeLabel.textColor = [UIColor greenColor];
    }
    return _timeLabel;
}
- (NSString *)getDateWithTimeInterval:(NSInteger)interval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];

    NSString *dateString = [formatter stringFromDate: date];
    return dateString;
}

- (void)dealloc
{
    [self.timer invalidate];
    self.timeLabel = nil;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
